ostree: Improve formatting for well-known summary metadata keys
authorPhilip Withnall <withnall@endlessm.com>
Thu, 4 May 2017 10:32:46 +0000 (11:32 +0100)
committerAtomic Bot <atomic-devel@projectatomic.io>
Mon, 8 May 2017 00:55:24 +0000 (00:55 +0000)
If a summary metadata key is well-known, like ostree.static-deltas, or
ostree.summary.last-modified, format it a little more nicely.

This is especially important for timestamps like last-modified, since
otherwise they’re formatted as a big-endian uint64, which is basically
unusable for the user.

Non-formatted output can still be retrieved using the OSTREE_DUMP_RAW
flag, and the non-formatted key name is always printed for clarity.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
Closes: #826
Approved by: cgwalters

src/ostree/ot-dump.c

index 4fc84cbd4806aa1de8cb46721b568f21dca08389..7cbc2945ee82da8d57637753e1a133b8723658eb 100644 (file)
@@ -199,6 +199,18 @@ dump_summary_ref (const char   *ref_name,
     }
 }
 
+static gchar *
+uint64_secs_to_iso8601 (guint64 secs)
+{
+  g_autoptr(GDateTime) dt = g_date_time_new_from_unix_utc (secs);
+  g_autoptr(GDateTime) local = (dt != NULL) ? g_date_time_to_local (dt) : NULL;
+
+  if (local != NULL)
+    return g_date_time_format (local, "%FT%T%:::z");
+  else
+    return g_strdup ("invalid");
+}
+
 void
 ot_dump_summary_bytes (GBytes          *summary_bytes,
                        OstreeDumpFlags  flags)
@@ -251,11 +263,35 @@ ot_dump_summary_bytes (GBytes          *summary_bytes,
 
   g_variant_iter_init (&iter, exts);
 
-  /* XXX Should we print something more human-friendly for
-   *     known extension names like 'ostree.static-deltas'? */
   while (g_variant_iter_loop (&iter, "{sv}", &key, &value))
     {
-      g_autofree char *string = g_variant_print (value, FALSE);
-      g_print ("%s: %s\n", key, string);
+      g_autofree gchar *value_str = NULL;
+      const gchar *pretty_key = NULL;
+
+      if (g_strcmp0 (key, "ostree.static-deltas") == 0)
+        {
+          pretty_key = "Static Deltas";
+          value_str = g_variant_print (value, FALSE);
+        }
+      else if (g_strcmp0 (key, "ostree.summary.last-modified") == 0)
+        {
+          pretty_key = "Last-Modified";
+          value_str = uint64_secs_to_iso8601 (GUINT64_FROM_BE (g_variant_get_uint64 (value)));
+        }
+      else if (g_strcmp0 (key, "ostree.summary.expires") == 0)
+        {
+          pretty_key = "Expires";
+          value_str = uint64_secs_to_iso8601 (GUINT64_FROM_BE (g_variant_get_uint64 (value)));
+        }
+      else
+        {
+          value_str = g_variant_print (value, FALSE);
+        }
+
+      /* Print out. */
+      if (pretty_key != NULL)
+        g_print ("%s (%s): %s\n", pretty_key, key, value_str);
+      else
+        g_print ("%s: %s\n", key, value_str);
     }
 }